package com.xyd.Servlet;

import com.xyd.Biz.CommodityBiz;
import com.xyd.Biz.FatherOrderBiz;
import com.xyd.Biz.Impl.CommodityBizImpl;
import com.xyd.Biz.Impl.FatherOrderBizImpl;
import com.xyd.Biz.Impl.OrderFormBizImpl;
import com.xyd.Biz.OrderFormBiz;
import com.xyd.Entity.Commodity;
import com.xyd.Entity.FatherOrder;
import com.xyd.Entity.OrderForm;
import com.xyd.Entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.*;
import java.io.IOException;
import java.util.List;

/**
 * @ClassName: ShowOrderServlet
 * @Description:
 * @Author 余全盛
 * @Date: 2021/10/20 10:28
 */
@WebServlet("/html/ShowOrderServlet")
public class ShowOrderServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        CommodityBiz commodityBiz = new CommodityBizImpl();
        List<Commodity> commodityList = commodityBiz.getAllCommodity();
        session.setAttribute("commodityList", commodityList);
        FatherOrderBiz fatherOrderBiz = new FatherOrderBizImpl();
        List<FatherOrder> orderList = fatherOrderBiz.selectAllFatherorderByUser(user);
        OrderFormBiz orderFormBiz = new OrderFormBizImpl();
        List<OrderForm> orderFormList = orderFormBiz.selectAllSonOrder();

        for (FatherOrder fatherOrder : orderList) {
            List<OrderForm> orderForms = orderFormBiz.selectSonOrder(fatherOrder.getFatherorderid());
            int i = 0;
            for (OrderForm orderForm : orderForms) {
                if (orderForm.getSonorderstate() == 4) {
                    i++;
                }
                if (i == orderForms.size()) {
                  int result=  fatherOrderBiz.updateFatherOrderState(4, fatherOrder.getFatherorderid());
                }
            }

        }
        orderList = fatherOrderBiz.selectAllFatherorderByUser(user);
        orderFormList = orderFormBiz.selectAllSonOrder();
        session.setAttribute("orderList", orderList);
        session.setAttribute("orderFormList", orderFormList);
        resp.sendRedirect("/shopping/html/showorder.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
